using Avalonia.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace AvaloniaDrawingPad.Models
{
    public class ShortcutItem : INotifyPropertyChanged
    {
        private string _name;
        private string _description;
        private Key _key;
        private KeyModifiers _modifiers;

        public string Name
        {
            get => _name;
            set
            {
                if (_name != value)
                {
                    _name = value;
                    OnPropertyChanged();
                }
            }
        }

        public string Description
        {
            get => _description;
            set
            {
                if (_description != value)
                {
                    _description = value;
                    OnPropertyChanged();
                }
            }
        }

        public Key Key
        {
            get => _key;
            set
            {
                if (_key != value)
                {
                    _key = value;
                    OnPropertyChanged();
                    OnPropertyChanged(nameof(KeyDisplay));
                }
            }
        }

        public KeyModifiers Modifiers
        {
            get => _modifiers;
            set
            {
                if (_modifiers != value)
                {
                    _modifiers = value;
                    OnPropertyChanged();
                    OnPropertyChanged(nameof(KeyDisplay));
                }
            }
        }

        public string KeyDisplay
        {
            get
            {
                string result = "";
                
                if (Modifiers.HasFlag(KeyModifiers.Control))
                    result += "Ctrl+";
                
                if (Modifiers.HasFlag(KeyModifiers.Shift))
                    result += "Shift+";
                
                if (Modifiers.HasFlag(KeyModifiers.Alt))
                    result += "Alt+";
                
                if (Key != Key.None)
                    result += Key.ToString();
                
                return result;
            }
        }

        public ShortcutItem(string name, string description, Key key, KeyModifiers modifiers = KeyModifiers.None)
        {
            _name = name;
            _description = description;
            _key = key;
            _modifiers = modifiers;
        }

        public event PropertyChangedEventHandler? PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class ShortcutManager
    {
        private static ShortcutManager? _instance;
        
        public static ShortcutManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new ShortcutManager();
                }
                return _instance;
            }
        }
        
        public ObservableCollection<ShortcutItem> Shortcuts { get; }
        
        private ShortcutManager()
        {
            Shortcuts = new ObservableCollection<ShortcutItem>
            {
                // 工具快捷键
                new ShortcutItem("画笔", "切换到画笔工具", Key.P, KeyModifiers.Control),
                new ShortcutItem("橡皮擦", "切换到橡皮擦工具", Key.E, KeyModifiers.Control),
                new ShortcutItem("直线", "切换到直线工具", Key.L, KeyModifiers.Control),
                new ShortcutItem("矩形", "切换到矩形工具", Key.R, KeyModifiers.Control),
                new ShortcutItem("圆形", "切换到圆形工具", Key.C, KeyModifiers.Control),
                new ShortcutItem("选择", "切换到选择工具", Key.S, KeyModifiers.Control),
                
                // 编辑快捷键
                new ShortcutItem("删除", "删除选中的图形", Key.Delete),
                new ShortcutItem("全选", "选择所有图形", Key.A, KeyModifiers.Control),
                new ShortcutItem("复制", "复制选中的图形", Key.C, KeyModifiers.Control | KeyModifiers.Shift),
                new ShortcutItem("粘贴", "粘贴复制的图形", Key.V, KeyModifiers.Control),
                new ShortcutItem("撤销", "撤销上一步操作", Key.Z, KeyModifiers.Control),
                new ShortcutItem("重做", "重做上一步撤销的操作", Key.Y, KeyModifiers.Control),
                
                // 文件操作快捷键
                new ShortcutItem("保存", "保存当前绘图", Key.S, KeyModifiers.Control | KeyModifiers.Shift),
                new ShortcutItem("导入图片", "导入图片到画布", Key.I, KeyModifiers.Control),
                new ShortcutItem("清空画布", "清空当前图层", Key.N, KeyModifiers.Control),
                
                // 图层操作快捷键
                new ShortcutItem("添加图层", "添加新图层", Key.N, KeyModifiers.Control | KeyModifiers.Shift),
                new ShortcutItem("上移图层", "上移当前图层", Key.Up, KeyModifiers.Control | KeyModifiers.Alt),
                new ShortcutItem("下移图层", "下移当前图层", Key.Down, KeyModifiers.Control | KeyModifiers.Alt),
                
                // 其他快捷键
                new ShortcutItem("显示快捷键", "显示快捷键设置", Key.F1)
            };
        }
        
        public ShortcutItem? FindShortcut(string name)
        {
            foreach (var shortcut in Shortcuts)
            {
                if (shortcut.Name == name)
                {
                    return shortcut;
                }
            }
            return null;
        }
        
        public ShortcutItem? FindShortcut(Key key, KeyModifiers modifiers)
        {
            foreach (var shortcut in Shortcuts)
            {
                if (shortcut.Key == key && shortcut.Modifiers == modifiers)
                {
                    return shortcut;
                }
            }
            return null;
        }
    }
} 